#!/usr/bin/env node

const https = require('https');
const fs = require('fs');
const path = require('path');

// 生成当前时间的毫秒级时间戳
const timestamp = Date.now();

// 构造请求 URL
const DM5_MANHUA_LIST_URL = `https://tel.dm5.com/manhua-list/dm5.ashx?t=${timestamp}`;

https.get(DM5_MANHUA_LIST_URL, {
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
  }
}, (res) => {
  console.log('状态码:', res.statusCode);
  console.log('响应头:', res.headers);
  let data = '';

  res.on('data', (chunk) => {
    data += chunk;
  });

  res.on('end', () => {
    if (res.statusCode === 200 && data.length === 0) {
      console.error('服务器没有返回任何数据。');
      return;
    }

    try {
      const comics = JSON.parse(data);
      console.log('漫画列表:', comics);
      // 这里可以添加处理漫画列表的逻辑
    } catch (err) {
      console.error('解析漫画列表失败:', err);
    }
  });
}).on('error', (err) => {
  console.error('请求遇到问题:', err);
});


function downloadImage(url, filename) {
  https.get(url, (res) => {
    const fileStream = fs.createWriteStream(path.join(__dirname, 'covers', filename));
    res.pipe(fileStream);

    fileStream.on('finish', () => {
      fileStream.close();
      console.log(`下载完成: ${filename}`);
    });
  }).on('error', (err) => {
    console.error(`下载图片失败: ${filename}`, err);
  });
}

